﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using OpenTK;
using OpenTK.Graphics.OpenGL;
namespace Imagiality.MeshProcessing
{
    public class ViewCamera
    {
        private Vector3 translation;
        private float upDown;
        private float leftRight;
        private float distanceFromObject;
        private Matrix4 lookAtMatrix;

        public ViewCamera()
        {
            upDown = 180;
            leftRight = 90;
            distanceFromObject = 10;
            initializeCameraPosition();
        }

        private void initializeCameraPosition()
        {
            translation = new Vector3(0f, 0f, 0f);
            updateLookAtMatrix();
        }

        private void updateLookAtMatrix()
        { 
            lookAtMatrix = Matrix4.LookAt(distanceFromObject, 0, 0,
                                          0,0,0,
                                          0,1,0);
        }

        public void Render(Scene scene)
        {
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            //Loading LookAtMatrix
            GL.MatrixMode(MatrixMode.Modelview);

            GL.LoadMatrix(ref lookAtMatrix);

            // Up Down left right setup
            GL.Translate(translation);
            GL.Rotate(upDown, Vector3d.UnitZ);
            GL.Rotate(leftRight, Vector3d.UnitY);

            // Look at Cube
            //Cube cube = new Cube(0.2f);
            //cube.Display();

            GL.Color3(Color.White);

            SceneQuery<Renderable> q = new RenderableParentsSceneQuery();

            IList<Renderable> result = q.Execute((IList<SceneObject>)scene.Objects.ToArray());

            for (int i = 0; i < result.Count; i++)
            {
                // will get ID here
                GL.PushName(result[i].GetID());
                result.ElementAt(i).Display();
                GL.PopName();
            }


        }

        public void AddToUpAndDown(float value)
        {
            upDown += value;
        }

        
        public void AddToDistanceFromObject(float term)
        {
            distanceFromObject += term;
            if (distanceFromObject <= 0)
                distanceFromObject = 0.2f;
            updateLookAtMatrix();
        }

        public void AddToLeftAndRight(float val)
        {
            if (upDown < 0)
            {
                upDown = Math.Abs(upDown);
                upDown = 360 - (upDown % 360);
            }

            if (upDown % 360 > 90 && upDown % 360 < 270)
                leftRight = leftRight - val;
            else
                leftRight = leftRight + val;
        }


        public void ChangeAngle(float diffX, float diffY, float diffZ)
        {

        }

        public void ChangePosition(float diffX, float diffY, float diffZ)
        {

        }
    }
}
